package net.nagaseyasuhito.nicola.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class Entry extends BaseEntity implements Comparable<Entry> {
	private static final long serialVersionUID = 1L;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "user__id")
	private User user;

	@Column(nullable = false)
	@Lob
	private String content;

	public Entry() {
	}

	public Entry(String content) {
		this(null, content);
	}

	public Entry(User user, String content) {
		this.setUser(user);
		this.setContent(content);
	}

	@Override
	public int compareTo(Entry entry) {
		if(this.getUpdateTimestamp() == null) {
			return -1;
		}
		if(entry.getUpdateTimestamp() == null) {
			return 1;
		}

		return -(this.getUpdateTimestamp().compareTo(entry.getUpdateTimestamp()));
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
}
